package com.huxiu.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huxiu.QunZuDetailActivity;
import com.huxiu.R;
import com.huxiu.listener.ListenerConstans;
import com.huxiu.listener.UpDataListviewListener;
import com.huxiu.listener.ViewPagerListener;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.NetworkHelper;
import com.huxiu.net.model.QunZuList;
import com.huxiu.net.responses.BaseResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.Global;
import com.huxiu.utils.LogUtil;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.LinkedHashMap;
import java.util.Map;


public class QunZuHolder implements OnClickListener {

    //    标题
    @ViewInject(R.id.item_mine_qunzu_title)
    private LinearLayout item_mine_qunzu_title;
    @ViewInject(R.id.title_jiaru_qun)
    private LinearLayout title_jiaru_qun;

    @ViewInject(R.id.title_jianli_qun)
    private LinearLayout title_jianli_qun;

    @ViewInject(R.id.item_linearLayout)
    private LinearLayout item_linearLayout;

    @ViewInject(R.id.qunzu_item_name)
    private TextView name;
    @ViewInject(R.id.qunzu_item_people_num)
    private TextView peopleNum;
    @ViewInject(R.id.qunzu_item_jianjie)
    private TextView jianjie;
    @ViewInject(R.id.qunzu_item_jiaru)
    private TextView jiaru;

    @ViewInject(R.id.qunzu_item_time)
    private TextView time;

    private int mCourpage;


    //我的群组没有内容时显示
    @ViewInject(R.id.qunzu_mine_nolist)
    private RelativeLayout qunzu_mine_nolist;
    @ViewInject(R.id.button_faxian_qunzu)
    private Button button_faxian_qunzu;
    @ViewInject(R.id.view_line)
    private View view_line;

    private static Activity mContext;
    private static WeakReference<BaseAdapter> mAdapter;

    public void setAdapter(BaseAdapter adapter) {
        mAdapter = new WeakReference<>(adapter);
    }

    private ViewPagerListener mViewPagerListener;

    private int mtotal;
    private int mposition;

    private int mytotal;
    private int myposition;


    public QunZuHolder(View view, Context context) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        if (ListenerConstans.mQunZuPager != null) {
            mViewPagerListener = ListenerConstans.mQunZuPager;
        }
    }

    private UpDataListviewListener mUpDataListviewListener;

    public QunZuHolder(View view, Context context, UpDataListviewListener listener) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        mUpDataListviewListener = listener;
    }

    public void bindItem(String item, int position) {
        button_faxian_qunzu.setOnClickListener(this);
        qunzu_mine_nolist.setVisibility(View.VISIBLE);
        item_linearLayout.setVisibility(View.GONE);
        view_line.setVisibility(View.GONE);
    }

    private QunZuList.ForumJoined mItem;

    public void bindItem(QunZuList.ForumJoined item, int position, int total, int page) {
        item_linearLayout.setOnClickListener(this);
        qunzu_mine_nolist.setVisibility(View.GONE);
        item_linearLayout.setVisibility(View.VISIBLE);
        view_line.setVisibility(View.VISIBLE);
        mItem = item;
        mytotal=total;
        myposition=position;
        this.mtotal=total;
        this.mposition=position;
        mCourpage = page;
        if (position == 0) {
            item_mine_qunzu_title.setVisibility(View.VISIBLE);
            title_jiaru_qun.setVisibility(View.VISIBLE);
            title_jianli_qun.setVisibility(View.GONE);
        } else if (position == (total-1)) {
            item_mine_qunzu_title.setVisibility(View.GONE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.GONE);
            view_line.setVisibility(View.GONE);
        } else if (position == total) {
            item_mine_qunzu_title.setVisibility(View.VISIBLE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.VISIBLE);
        } else {
            item_mine_qunzu_title.setVisibility(View.GONE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.GONE);
        }
        name.setText(mItem.name);
        time.setText(Utils.getDateString(mItem.update_time));
    }

    private QunZuList.ForumRecomment mItem2;

    public void bindItem(QunZuList.ForumRecomment item, int position, int total, int group) {
        item_linearLayout.setOnClickListener(this);
        jiaru.setOnClickListener(this);
        mItem2 = item;
        mCourpage = group;
        if (position == 0){
            item_mine_qunzu_title.setVisibility(View.VISIBLE);
            title_jiaru_qun.setVisibility(View.VISIBLE);
            title_jianli_qun.setVisibility(View.GONE);
        } else if (position == (total-1)) {
            item_mine_qunzu_title.setVisibility(View.GONE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.GONE);
            view_line.setVisibility(View.GONE);
        } else if (position == total) {
            item_mine_qunzu_title.setVisibility(View.VISIBLE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.VISIBLE);
        } else {
            item_mine_qunzu_title.setVisibility(View.GONE);
            title_jiaru_qun.setVisibility(View.GONE);
            title_jianli_qun.setVisibility(View.GONE);
        }
        name.setText(mItem2.name);
        peopleNum.setText(mItem2.user_total + "个成员");
        jianjie.setText(mItem2.desc);
        jiaru.setText(mItem2.joined() ? "已加入" : "+加入");
        jiaru.setTextColor(mContext.getResources().getColor(mItem2.joined()
                ? R.color.gray3 : R.color.white));
        jiaru.setBackgroundColor(mItem2.joined() ? 0x00000000 : 0xff0DA3FA);
        jiaru.setEnabled(item.is_join == 1 ? false : true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_linearLayout:
                if (mCourpage == Constants.GROUP_OF_FINDGROUP) {
                    Intent intent = new Intent(mContext, QunZuDetailActivity.class);
                    intent.putExtra(Constants.GROUP_FINDGROUP_ITEM_KEY, mItem2.toString());
                    Log.e("mItem", "mItem" + mItem2.toString());
                    mContext.startActivity(intent);
                } else if (mCourpage == Constants.GROUP_OF_MYGROUP) {
                    if(myposition<mytotal){

                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_LOGIN_JOIN_GROUP);
                    }else {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_LOGIN_BUILD_GROUP);
                    }
                    Intent intent = new Intent(mContext, QunZuDetailActivity.class);
                    intent.putExtra(Constants.GROUP_MYGROUP_ITEM_KEY, mItem.toString());
                    mContext.startActivity(intent);
                }
                break;
            case R.id.button_faxian_qunzu:
                if (mViewPagerListener != null) {
                    mViewPagerListener.setCurrentItem(1, ListenerConstans.mFaXian);
                }
                break;
            case R.id.qunzu_item_jiaru:
                if(mposition<mtotal){
                    UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_UNLOGIN_JOINRECOMMEND);
                }else {
                    UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_UNLOGIN_JOINHOT);
                }
                if (!Settings.isLoggedIn()) {
                    Utils.showToast("请先登录");
                    Utils.askLogIn(mContext);
                    return;
                }
                joinItem(mItem2, mAdapter);
                break;
        }
    }


    public void joinItem(final QunZuList.ForumRecomment item, final WeakReference<BaseAdapter> adapter) {
        String url = NetworkConstants.FORUM_JOIN_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.uid);
        params.put("id", String.valueOf(item.id));
        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                if (response.isSuccess()) {
                    if (item.is_join == 1) {
                        item.is_join = 0;
                    } else {
                        item.is_join = 1;
                        Utils.showToast(R.string.qunzu_qunzu_add_ok);
                    }
                    if (adapter != null && adapter.get() != null) {
                        Utils.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                jiaru.setText(item.is_join == 1 ? "已加入" : "+加入");
                                jiaru.setTextColor(mContext.getResources().getColor(item.is_join == 1
                                        ? R.color.gray3 : R.color.white));
                                jiaru.setBackgroundColor(item.is_join == 1 ? 0x00000000 : 0xff0DA3FA);
                                jiaru.setEnabled(item.is_join == 1 ? false : true);
                                adapter.get().notifyDataSetChanged();
                            }
                        });
                    }
                    mUpDataListviewListener.updataData(item.id, item.is_join);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(error.getMessage());
            }
        });
        NetworkHelper.getInstance().addToRequestQueue(request);
    }
}


